<?php
class KeptAction extends SystemCommonAction{
	function config(){
		$this->checkMember();
		$this->assign('is',C('KEPT_IS'));
		$this->assign('is2',C('COMMENTS_IS'));
		$this->assign('indexlist',C('KEPT_PAGE'));
		$this->assign('conlist',C('COMMENTS_PAGE'));
		$this->display();
	}

	function post(){
		$this->checkMember();
		if($_POST['get']=='config'){
			//$a=include '/Conf/config.php';
			//$a['KEPT_IS']=$_POST['KEPT_IS'];
			if($this->config_insert()){
				$this->success("配置成功！");
			};//执行配置文件写入
		}else if($_POST['get']=='con'){
			if($this->conxiu()){
				$this->success("修改成功！");
			}else{
				$this->error("操作失败！");

			};//执行配置文件写入
		}else if($_POST['get']=='p'){
		    if($this->conxiup()){
				$this->success("修改成功！");
			}else{
				$this->error("操作失败！");

			};//执行配置文件写入
		}
	}
	private function conxiu(){
		$kept = M("kept");
		$data['title'] =$_POST['title'];
		$data['is'] = $_POST['is'];
		$data['brand'] = $_POST['brand'];
		$data['category'] = $_POST['category'];
		$data['commodity'] = $_POST['commodity'];
		$data['con'] = stripcslashes($_POST['con']);
		$data['source'] = $_POST['source'];
		$data['url'] = $_POST['url'];
		$where['id']=$_POST['id'];
		$arr=$kept->where($where)->save($data); // 根据条件保存修改的数据
		if($arr){
			return true;
		}else{
			return false;
		}
	}
	
	
private function conxiup(){
		$kept = M("kept_comments");
		$data['is'] = $_POST['is'];
		$data['con'] = $_POST['con'];
		$where['id']=$_POST['id'];
		$arr=$kept->where($where)->save($data); // 根据条件保存修改的数据
		if($arr){
			return true;
		}else{
			return false;
		}
	}

	private function config_insert(){
		$this->checkMember();
		$fp=fopen("Conf/config.php","r");
		$str=fread($fp,filesize("Conf/config.php"));
		$str=str_replace("'KEPT_IS'=>".C('KEPT_IS'),"'KEPT_IS'=>".$_POST['KEPT_IS'],$str);//写入审核配置
		$str=str_replace("'KEPT_PAGE'=>".C('KEPT_PAGE'),"'KEPT_PAGE'=>".$_POST['indexliset'],$str);//写入首页列表条数配置
		$str=str_replace("'COMMENTS_PAGE'=>".C('COMMENTS_PAGE'),"'COMMENTS_PAGE'=>".$_POST['conlist'],$str);//写入评论列表条数配置
		$str=str_replace("'COMMENTS_IS'=>".C('COMMENTS_IS'),"'COMMENTS_IS'=>".$_POST['COMMENTS_IS'],$str);//写入评论审核配置
		$handle=fopen('Conf/config.php',"w");
		fwrite($handle,$str);
		fclose($handle);
		return true;
	}

	function kept_list(){
		$this->checkMember();
		$this->admin_kept_list();
		$this->display();
	}
	private function admin_kept_list(){
		$this->checkMember();
		$sql = M("kept");
		import("ORG.Util.Page");
		$count      = $sql->count();
		$Page       = new Page($count,20);
		$show       = $Page->show();
		if($_POST['date1']){
			$where['data']=array('gt',strtotime(date($_POST['date1'], time())));
		}
		if($_POST['date2']){
			$where['data']=array('lt',strtotime(date($_POST['date2'], time())));
		}
		if($_POST['date1'] && $_POST['date2']){
			$where['data']=array(array('gt',strtotime(date($_POST['date1'], time()))),array('lt',strtotime(date($_POST['date2'], time())))) ;
		}
		if($_POST['key']){
			$where['title']=array('like','%'.$_POST['key'].'%');
		}
		$list = $sql->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$i=-1;
		foreach ($list as $key =>$value){
			$i++;
			$User = M("User");
			$condition['id'] = $value['userid'];
			$arr=$User->where($condition)->select();
			$list[$i]['user']=$arr[0]["user_name"];
			$sql2=M('Kept_comments');
			$wheres['typeid']=$value['id'];
			$row=$sql2->where($wheres)->count();
			$list[$i]['comments']=$row;
			//$list[i]['user']=$arr[0]["user_name"];
		}
		$this->assign('kept_list',$list);
		$this->assign('page',$show);
	}
	function shan(){
		$this->checkMember();
		if($this->shanchu()){
			$this->success("操作成功！");
		}
	}
	function shen(){
		$this->checkMember();
		if($this->shenhe()){
			$this->success("修改成功！");
		}
	}

	function shens(){
		$this->checkMember();
		if($this->shenhes()){
			$this->success("修改成功！");
		}
	}

	private function shanchu(){
		$this->checkMember();
		$sql = M("kept");
		$arr=$sql->where("id in (" .$_GET[id].")")->delete();
		if($arr){
			return true;
		}
	}
	private function shenhe(){
		$this->checkMember();
		$sql = M("kept");
		$data['is']=1;
		$arr=$sql->where("id in (" .$_GET[id].")")->save($data);
		if($arr){
			return true;
		}
	}

	private function shenhes(){
		$this->checkMember();
		$sql = M("kept");
		$data['is']=0;
		$arr=$sql->where("id in (" .$_GET[id].")")->save($data);
		if($arr){
			return true;
		}
	}

	function xiugai(){
		$this->checkMember();
		$sql = M("kept");
		$where['id']=$_GET['id'];
		$arr=$sql->where($where)->select();
		$this->assign('arr',$arr);
		$this->assign('is',$arr[0]['is']);
		$this->display();
	}
	function kept_comments(){
		$this->checkMember();
		$sql = M("kept_comments");
		import("ORG.Util.Page");
		$count      = $sql->count();
		$Page       = new Page($count,20);
		$show       = $Page->show();
		if($_POST['date1']){
			$where['data']=array('gt',strtotime(date($_POST['date1'], time())));
		}
		if($_POST['date2']){
			$where['data']=array('lt',strtotime(date($_POST['date2'], time())));
		}
		if($_POST['date1'] && $_POST['date2']){
			$where['data']=array(array('gt',strtotime(date($_POST['date1'], time()))),array('lt',strtotime(date($_POST['date2'], time())))) ;
		}
		if($_POST['key']){
			$where['title']=array('like','%'.$_POST['key'].'%');
		}
		$list = $sql->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$i=-1;
		foreach ($list as $key =>$value){
			$i++;
			$User = M("User");
			$condition['id'] = $value['userid'];
			$arr=$User->where($condition)->select();
			$list[$i]['user']=$arr[0]["user_name"];
			$sql2=M('kept');
			$wheres['id']=$value['typeid'];
			$row=$sql2->where($wheres)->select();
			$list[$i]['title']=$row[0]['title'];
			//$list[i]['user']=$arr[0]["user_name"];
		}
		$this->assign('kept_list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	function shanp(){
		$this->checkMember();
		if($this->shanchup()){
			$this->success("操作成功！");
		}
	}
	private function shanchup(){
		$this->checkMember();
		$sql = M("kept_comments");
		$arr=$sql->where("id in (" .$_GET[id].")")->delete();
		if($arr){
			return true;
		}
	}
	function shensp(){
		$this->checkMember();
		if($this->shenhesp()){
			$this->success("修改成功！");
		}
	}
	private function shenhesp(){
		$this->checkMember();
		$sql = M("kept_comments");
		$data['is']=0;
		$arr=$sql->where("id in (" .$_GET[id].")")->save($data);
		if($arr){
			return true;
		}
	}
	function shenp(){
		$this->checkMember();
		if($this->shenhep()){
			$this->success("修改成功！");
		}
	}
	private function shenhep(){
		$this->checkMember();
		$sql = M("kept_comments");
		$data['is']=1;
		$arr=$sql->where("id in (" .$_GET[id].")")->save($data);
		if($arr){
			return true;
		}
	}

	function xiugaip(){
		$this->checkMember();
		$sql = M("kept_comments");
		$where['id']=$_GET['id'];
		$arr=$sql->where($where)->select();
		$this->assign('arr',$arr);
		$this->assign('is',$arr[0]['is']);
		$this->display();
	}

}